package com.baigt.juc.base;

import java.util.concurrent.TimeUnit;

/**
 * 类功能描述:
 * 中断和sleep
 *
 * @author baigt
 * @version V1.0
 * @since 2019-10-21 10:25
 */
public class ThreadRun04 {
    public static void main(String[] args) throws InterruptedException {
        interrupt2();
    }

    public static void interrupt() throws InterruptedException {
        Thread t = new Thread("ricky") {
            @Override
            public void run() {
                while (true) {
                    // do nothing just empty loop
                }
            }
        };
        t.start();
        TimeUnit.SECONDS.sleep(10L);
        System.out.println("Thread is interrupted :" + t.isInterrupted());
        t.interrupt();
        System.out.println("Thread is interrupted :" + t.isInterrupted());
    }

    public static void interrupt2() throws InterruptedException {
        Thread t = new Thread("ricky") {
            @Override
            public void run() {
                while (true) {
                    try {
                        TimeUnit.MINUTES.sleep(1);
                    } catch (InterruptedException e) {
                        System.out.println("i am interrupted " + isInterrupted());
                    }
                }
            }
        };
        t.start();
        TimeUnit.MILLISECONDS.sleep(20L);
        System.out.println("Thread is interrupted :" + t.isInterrupted());
        t.interrupt();
        TimeUnit.MILLISECONDS.sleep(20L);
        System.out.println("Thread is interrupted :" + t.isInterrupted());
    }
}
